package com.mhxy.controller;

import com.mhxy.constant.TaskConstants;
import com.mhxy.handler.ConfigHandler;
import com.mhxy.util.CommandUtil;
import com.mhxy.util.HwndUtil;
import com.mhxy.util.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class OpenController {
    
    public static void run(boolean isLogin) throws Exception {
        // 打开游戏窗口
        openGameByBox();
        // 按Box名称加载游戏窗口句柄
        HwndUtil.loadAllGameHwnd(ConfigHandler.getBoxNames());
        // 调整窗口大小和位置
        HwndUtil.setWindowPost();
        // 登录游戏账号
        loginGame(isLogin);
    }



    public static void openGameByBox() throws Exception {
        for (String boxName : ConfigHandler.getBoxNames()) {
            CommandUtil.openGameByBox(boxName);
        }
        ThreadUtil.sleep(10000);
    }

    public static void loginGame(boolean isLogin) throws Exception {
        if (!isLogin) {
            return;
        }
        ThreadUtil.sleep(5000);
        HwndUtil.hwndsMap.keySet().forEach(k -> {
            StartController.run(k, Collections.singletonList(TaskConstants.LOGIN_GAME));
        });
    }


}
